package LeetCode.leetcode530;

import java.util.ArrayList;
import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    int min = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        test(root,list);
        return min;
    }
    public void test(TreeNode node, List<Integer> list){
        if (node == null) return;
        test(node.left,list);
        if (list.size() > 0) {
            if (node.val - list.get(list.size() - 1) < min) min = node.val - list.get(list.size() - 1);
        }
        list.add(node.val);
        test(node.right,list);
    }
}